package com.bw.android.bwgao6week2a;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;

import com.bw.android.bwgao6week2a.adapter.MyPagerAdapter;
import com.bw.android.bwgao6week2a.databinding.ActivityHomeBinding;
import com.bw.android.bwgao6week2a.fragment.AnimationFragment;
import com.bw.android.bwgao6week2a.fragment.AuthFragment;
import com.bw.android.bwgao6week2a.fragment.BlankFragment;
import com.bw.android.bwgao6week2a.fragment.PrepareLiveFragment;
import com.bw.android.bwgao6week2a.fragment.HomeFragment;
import com.bw.android.bwgao6week2a.vm.HomeTitleViewModel;
import com.bw.ch.baselib.BaseBindingActivity;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HomeActivity extends BaseBindingActivity<ActivityHomeBinding> {

    public static final List<String> tabLabels = new ArrayList<>(Arrays.asList("首页", "直播", "动画", "OCR", "我的"));
    private final List<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter myPagerAdapter;

    private HomeTitleViewModel homeTitle;

    @Override
    public int getLayout() {
        return R.layout.activity_home;
    }

    @Override
    protected void initView() {
        mFragments.add(new HomeFragment());
        mFragments.add(new PrepareLiveFragment());
        mFragments.add(new AnimationFragment());
        mFragments.add(new AuthFragment());
        for (int i=mFragments.size(); i<tabLabels.size(); i++) {
            mFragments.add(new BlankFragment());
        }
//        myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(), mFragments, tabLabels);
//        mBinding.homeViewPager.setAdapter(myPagerAdapter);
        mBinding.homeViewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return tabLabels.size();
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return tabLabels.get(position);
            }
        });
        mBinding.homeViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }
            @Override
            public void onPageSelected(int position) {
                //实现周考3的第3点需求
                homeTitle.title.setValue(tabLabels.get(position));
                //实现周考3的第4、5点需求
                switch (position) {
                    case 0:
                        mBinding.tvGoLive.setVisibility(View.VISIBLE);
                        mBinding.ivLive.setVisibility(View.GONE);
                        break;
                    case 1:
                        mBinding.tvGoLive.setVisibility(View.GONE);
                        mBinding.ivLive.setVisibility(View.VISIBLE);
                        break;
                    default:
                        mBinding.tvGoLive.setVisibility(View.GONE);
                        mBinding.ivLive.setVisibility(View.GONE);
                        break;
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        mBinding.homeTabLayout.setupWithViewPager(mBinding.homeViewPager, true);
        mBinding.homeTabLayout.removeAllTabs();
        for (int i=0; i<tabLabels.size(); i++) {
            TabLayout.Tab tab = mBinding.homeTabLayout.newTab();
            tab.setIcon(R.mipmap.ic_launcher);
            tab.setText(tabLabels.get(i));
            mBinding.homeTabLayout.addTab(tab);
        }
    }
    @Override
    protected void initData() {
        homeTitle = new ViewModelProvider(this).get(HomeTitleViewModel.class);
        mBinding.setHomeTitle(homeTitle);
        homeTitle.title.setValue(tabLabels.get(0));
    }
}